<template>
  <a-card :bordered="false">
    <a-spin :spinning="isLoading">
      <div class="card-title">{{ $route.meta.title }}</div>
      <div class="table-operator">
        <!-- 搜索板块 -->
        <a-row class="row-item-search">
          <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
            <a-form-item label="订单查询">
              <a-input style="width: 342px" placeholder="请输入关键词" v-decorator="['searchValue']">
                <a-select
                  slot="addonBefore"
                  v-decorator="['searchType', { initialValue: 10 }]"
                  style="width: 100px"
                >
                  <a-select-option
                    v-for="(item, index) in SearchTypeEnum"
                    :key="index"
                    :value="item.value"
                  >{{ item.name }}</a-select-option>
                </a-select>
              </a-input>
            </a-form-item>

            <a-form-item label="下单时间">
              <a-range-picker format="YYYY-MM-DD" v-decorator="['betweenTime']" />
            </a-form-item>
            <a-form-item class="search-btn">
              <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
            </a-form-item>
            <a-form-item class="search-btn">
              <a-button @click="handleReset">重置</a-button>
            </a-form-item>
            <a-form-item class="search-btn">
              <a-button icon="download" @click="download" type="dashed">导出</a-button>
            </a-form-item>
          </a-form>
        </a-row>

      </div>
      <!-- 列表内容 -->
      <div class="ant-table ant-table-scroll-position-left ant-table-default ant-table-bordered">
        <div class="ant-table-content">
          <div class="ant-table-body">
            <table>
              <thead class="ant-table-thead">
                <tr>
                  <th v-for="(item, index) in columns" :key="index">
                    <span class="ant-table-header-column">
                      <div>
                        <span class="ant-table-column-title">{{ item.title }}</span>
                      </div>
                    </span>
                  </th>
                </tr>
              </thead>
              <tbody class="ant-table-tbody">
                <template v-for="(item) in orderList.data">
                  <tr class="order-empty" :key="`order_${item.patch_id}_1`">
                    <td colspan="8"></td>
                  </tr>
                  <tr :key="`order_${item.patch_id}_2`">
                    <td colspan="8">
                      <span class="mr-20">[{{ item.patch_id }}]</span>
                      <span class="mr-20">{{ item.create_time }}</span>
                      <span>订单号：{{ item.order_no }}</span>
                    </td>
                  </tr>

                  <tr :key="`order_${item.patch_id}_3`">
                    <td> <UserItem :user="item.user" />

                    </td>
                    <td>
                      [{{ item.shop_region }}]{{ item.name }}-{{ item.phone }}
                    </td>
                    <td>{{ item.shop_name }}</td>
                    <td>{{ item.patch_model }}</td>
                    <td >
                      <div style="max-width:150px;word-break:break-all;white-space: pre-wrap;" v-html=" item.buyer_remark "></div>
                    </td>
                    <td >
                      <div style="max-width:150px;word-break:break-all;white-space: pre-wrap;" v-html=" item.remark "></div>
                    </td>
                    <td>
                      <p
                        class="mtb-2"
                      >
                        <span class="f-13">订单状态：</span>
                        <a-tag
                          :color="PatchStatusEnum[item.order_status].color"
                        >{{ PatchStatusEnum[item.order_status].name }}</a-tag>
                      </p>
                      <p class="mtb-2" v-if="item.order_status==PatchStatusEnum.CHECK_SUCCESS.value">
                        <span class="f-13">发货状态：</span>
                        <a-tag
                          :color="item.delivery_status == DeliveryStatusEnum.DELIVERED.value ? 'green' : ''"
                        >{{ DeliveryStatusEnum[item.delivery_status].name }}</a-tag>
                      </p>

                      <p class="mtb-2" v-if="item.order_status==PatchStatusEnum.CHECK_SUCCESS.value">
                        <span class="f-13">收货状态：</span>
                        <a-tag
                          :color="item.receipt_status == ReceiptStatusEnum.RECEIVED.value ? 'green' : ''"
                        >{{ ReceiptStatusEnum[item.receipt_status].name }}</a-tag>
                      </p>

                    </td>
                    <td>
                      <div class="actions">
                        <!-- <router-link
                          v-if="$auth('/patch/detail')"
                          :to="{ path: '/patch/detail', query: { orderId: item.patch_id } }"
                        >查看详情</router-link> -->
                        <a
                          v-if="$auth('/patch/list/all.detail')"
                          @click="goShowDetail(item.patch_id)"
                        >查看详情</a>

                      </div>
                    </td>

                  </tr>

                </template>
              </tbody>
            </table>
          </div>
          <!-- 空状态 -->
          <a-empty v-if="!orderList.data.length" :image="simpleImage" />
        </div>
      </div>
      <!-- 分页器 -->
      <div v-if="orderList.data.length" class="pagination">
        <a-pagination
          :current="page"
          :pageSize="orderList.per_page"
          :total="orderList.total"
          @change="onChangePage"
        />
      </div>
      <!-- <DeliveryForm ref="DeliveryForm" @handleSubmit="handleRefresh" />
      <CancelForm ref="CancelForm" @handleSubmit="handleRefresh" /> -->
      <PatchDetail ref="PatchDetail" @handleSubmit="handleRefresh" />
    </a-spin>
  </a-card>
</template>

<script>
import { Empty } from 'ant-design-vue'
import { inArray, assignment } from '@/utils/util'
import * as Api from '@/api/patch'
import { GoodsItem, UserItem } from '@/components/Table'
import {
  DeliveryStatusEnum,
  DeliveryTypeEnum,
  OrderSourceEnum,
  PayStatusEnum,
  PatchStatusEnum,
  PayTypeEnum,
  ReceiptStatusEnum
} from '@/common/enum/order'
import { PatchDetail } from './modules'

// 表格字段
const columns = [
   {
    title: '用户',
    dataIndex: 'user'
  },
  {
    title: '用户信息',
    align: 'center',
    dataIndex: 'shop_name',
    scopedSlots: { customRender: 'goods' }
  },
  {
    title: '店铺名称',
    align: 'center',
    dataIndex: 'shop_name',
    scopedSlots: { customRender: 'goods' }
  },
  {
    title: '产品型号',
    align: 'center',
    dataIndex: 'patch_model',
    scopedSlots: { customRender: 'goods' }
  },
  {
    title: '补件说明',
    align: 'center',
    scopedSlots: { customRender: 'unit_price' }
  },
   {
    title: '客服备注',
    align: 'center',
    scopedSlots: { customRender: 'remark' }
  },
  {
    title: '订单状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' }
  }
]

// 搜索关键词类型枚举
const SearchTypeEnum = [
  { name: '会员昵称', value: 10 },
  { name: '店铺名称', value: 20 },
  { name: '会员ID', value: 30 },
    { name: '手机号码', value: 40 }
]

export default {

  components: {
    GoodsItem,
    UserItem,
    // DeliveryForm,
    // CancelForm,
      PatchDetail
  },
  data () {
    return {
      // 订单类型
      dataType: this.getDataType(),
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      showDetail: false,
      // 正在加载
      isLoading: false,
      showDetailId: 0,
      // 表格字段
      columns,
      // 当前页码
      page: 1,
      // 列表数据
      orderList: { data: [], total: 0, per_page: 10 }
    }
  },
  beforeCreate () {
    // 批量给当前实例赋值
    assignment(this, {
      inArray,
      DeliveryStatusEnum,
      DeliveryTypeEnum,
      OrderSourceEnum,
      PatchStatusEnum,
      PayStatusEnum,
      PayTypeEnum,
      ReceiptStatusEnum,
      SearchTypeEnum,
      simpleImage: Empty.PRESENTED_IMAGE_SIMPLE
    })
  },
  created () {
    // 初始化页面
    this.init()
  },
  watch: {
    // 监听路由变化
    $route () {
      this.init()
    }
  },
  methods: {

  goShowDetail (patchId) {
     this.$refs.PatchDetail.show(patchId)
  },
    // 初始化页面
    init () {
      this.dataType = this.getDataType()
      this.searchForm.resetFields()
      this.queryParam = {}
      this.handleRefresh(true)
    },

    // 获取当前标签
    getDataType () {
      return this.$route.path.split('/')[3]
    },

    // 获取列表数据
    getList () {
      const { dataType, queryParam, page } = this
      this.isLoading = true
      return Api.list({ dataType, ...queryParam, page })
        .then(response => {
          this.orderList = response.data.list
        })
        .finally(() => {
          this.isLoading = false
        })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      bool && (this.page = 1)
      this.getList()
    },

    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

  download () {
    const app = this
  this.$confirm({
          title: '单次最多导出1000条数据,超出部分请按时间条件进行筛选',
          onOk () {
                app.searchForm.validateFields((error, values) => {
              if (!error) {
                app.queryParam = { ...app.queryParam, ...values }
                const { dataType, queryParam, page } = app
                app.isLoading = true
                return Api.list({ dataType, ...queryParam, page, export: 1 })
                  .then(response => {
                    window.open(response.data.list, '_blank')
                    console.log('导出', response)
                  })
                  .finally(() => {
                    app.isLoading = false
                  })
              }
            })
          },
          onCancel () {
            console.log('Cancel')
          },
          class: 'test'
        })
  },
    // 重置搜索表单
    handleReset () {
      this.searchForm.resetFields()
    },

    // 翻页事件
    onChangePage (current) {
      this.page = current
      this.handleRefresh()
    },

    // 订单发货
    handleDelivery (record) {
      this.$refs.DeliveryForm.show(record)
    },

    // 审核取消订单
    handleCancel (record) {
      this.$refs.CancelForm.show(record)
    }

  }
}
</script>
<style lang="less" scoped>
// 分页器
.pagination {
  margin-top: 16px;
  .ant-pagination {
    float: right;
  }
}

.ant-table {
  table {
    border: none;
    border-collapse: collapse;
  }
  .ant-table-thead > tr {
    border: 1px solid #e8e8e8;
  }
  tr.order-empty {
    height: 15px;
    border: 1px solid #fff;
    td {
      padding: 0;
      border-right: none;
      border-left: none;
      background: none !important;
    }
  }
}

.ant-table-thead > tr > th {
  border-right: none;
  border-bottom: none;
  padding: 12px 12px;
  font-weight: bold;
}
.ant-table-tbody > tr > td {
  border-right: 1px solid #e8e8e8;
  border-left: 1px solid #e8e8e8;
  padding: 12px 12px;
  // text-align: center;
}
</style>
